class Giving {
    /**
     * 获取奖励信息
     * @api {GET}         /giving/giving             获取奖励信息
     * @apiName           /giving/giving
     * @apiGroup          Giving
     * @apiVersion 0.1.0
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": {
            "registered": 1,
            "shop": 1,
            "day": 3
        }
       }
     */
    async giving(ctx) {
        let {
            db,
            user: { User }
        } = ctx;

        let condition1 = {
            where: {
                userId: User.id,
                isShop: 0
            }
        };
        let condition2 = {
            where: {
                userId: User.id,
                isShop: 1
            }
        };
        let condition3 = {
            where: {
                givingId: User.id,
                isShop: 1
            }
        };
        let registered = await db.Reward.count(condition1);
        let shop = await db.Reward.count(condition2);
        let day = await db.Reward.sum('day', condition3);
        ctx.success({ registered: registered, shop: shop, day: day });
    }
    /**
     * 获取邀请分享
     * @api {GET}         /giving/share             获取邀请分享
     * @apiName           /giving/share
     * @apiGroup          Giving
     * @apiVersion 0.1.0
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": {
            "registered": [
                {
                    "id": 1,
                    "limit": 10,
                    "desc": "成功邀请注册，赠送10次洗车",
                    "type": -1,
                    "createdAt": "2019-06-12T09:54:01.000Z",
                    "updatedAt": "2019-06-12T12:16:48.000Z"
                }
            ],
            "shop": [
                {
                    "id": 2,
                    "day": 3,
                    "desc": "成功邀请注册并购买会员，赠送三天会员",
                    "type": 1,
                    "createdAt": "2019-06-12T09:54:44.000Z",
                    "updatedAt": "2019-06-12T09:54:47.000Z"
                }   "updatedAt": "2019-06-12T12:17:07.000Z"
            ],
            "roles": {
                "membersRoles": "abc"
            }
        }
     }
     */
    async share(ctx) {
        let { db } = ctx;

        let registered = await db.Giving.findAll({
            where: { id: 1, type: -1 }
        });

        let shop = await db.Giving.findAll({ where: { type: 1 } });

        let roles = await db.Instructions.findOne({
            where: { id: 1 },
            attributes: ['shareRoles']
        });

        ctx.success({ registered: registered, shop: shop, roles });
    }
}
module.exports = new Giving();
